<--- %%NOBANNER%% --> _ranchi.sas
 BackForward

/*-------------------<-- Start of Description -->--------------------\
| Generate random variate from a Chisq distribution                  |
|--------------------<--- End of Description -->---------------------|
|--------------------------------------------------------------------|
|--------------<--- Start of Files or Arguments Needed -->-----------|
| Arguments Need:                                                    |
|      seed - seed; Required, default is the current system time;    |
|      var  - the variable to save the generated random variates;    |
|      df   - degree of freedom;                                     |
|      Note: it is also a gamma distribution with alpha=df/2 and     |
|            beta=2;                                                 |
|      temp - the temporary variable to update the seed;             |
|             default is _ranchi0_;                                  |
|---------------<--- End of Files or Arguments Needed -->------------|
|--------------------------------------------------------------------|
|----------------<--- Start of Example and Usage -->-----------------|
|Example                                                             |
|   data one;                                                        |
|      do i=1 to 20;                                                 |
|      x=%_ranchi(seed=1, df=3);                                     |
|      z=%_ranchi(seed=1, df=3);                                           |
|      %_ranchi(seed=12345,var=y, df=3);                             |
|      output;                                                       |
|      end;                                                          |
|   run; %print(one);                                                |
|Usage: %_ranchi(seed=%sysfunc(datetime(), 15.), df=REQUIRED, var=,  |
|                temp=_ranchi0_);                                    |
\-------------------<--- End of Example and Usage -->---------------*/
%macro _ranchi(seed=%sysfunc(datetime(), 15.), df=REQUIRED, var=, 
               temp=_ranchi0_);
/*--------------------------------------------\
| Author:  Duo Zhou;                          |
| Created: 3-22-2002 6:30pm;                  |
| Purpose: Random Chisq Generator;            |
\--------------------------------------------*/
%let _dfchk_=%sysfunc(rxmatch(%sysfunc(rxparse($a|$A|$w)),&df));
%if (%quote(&seed) eq) or &_dfchk_ %then %do;
   %if (%quote(&seed) eq) %then %do;
      %put ==> Error: This is not a valid seed!; 
      %if (%length(&var)) %then %do; &var=.; %end;
      %else %do; .;%end;
   %end;
   %if &_dfchk_ %then %do;
      %put ==> Error: &df is not a valid degree of freedom!; 
      %if (%length(&var)) %then %do; &var=.; %end;
      %else %do; .;%end;
   %end;
   %goto finish;
%end;
%else %do;  
   %if (%length(&var)) %then %do;
      %if (not %sysfunc(rxmatch(%sysfunc(rxparse(_|.|$a|$A|$w)),&seed))) %then %do;
         drop &temp;
         retain &temp &seed;
         %let seed=&temp;
      %end;
      call rangam(&seed, &df/2, &var);
      &var=&var*2;
   %end;
   %else 2*rangam(&seed, &df/2);
%end;
%finish:
%mend _ranchi;